﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _5_02_异常处理
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string[] arr = new string[5];
                arr[5] = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现异常，信息为：" + ex.Message);
            }
            finally//不管是否出现异常，最终都要执行的代码
            {

            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                //模拟下标越界异常
                string[] arr = new string[5];
                arr[4] = "";
                //模拟书记库操作异常
                string sql = "server=.;database=DBTEST;uid=sa;pwd=200234.A";
                SqlConnection sqlConnection = new SqlConnection(sql);
                sqlConnection.Open();
                sqlConnection.Close();
            }
            catch (System.IndexOutOfRangeException ex)
            {
                string strErr = "程序出现问题" + ex.Message + '\n';
                //strErr +=""
                MessageBox.Show(strErr);
            }
            catch (System.ArgumentException ex)
            {
                MessageBox.Show("出现异常，信息为：" + ex.Message);
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                MessageBox.Show("出现异常，信息为：" + ex.Message);
            }
            catch(Exception ex)
            {
                MessageBox.Show("出现异常，信息为：" + ex.Message);
            }
            finally//不管是否出现异常，最终都要执行的代码
            {

            }

        }
    }
}
